library(plotly)
df_link <- "https://github.com/vetszabolcs/suicide_data/raw/main/master.csv"
df <- read.csv(df_link)
colnames(df)[1] <- "country"
aggreGate <- function(data_f, by="country", by2="year", new_col){
new_df <- data_f
for (x in unique(new_df[,by])){
for (y in unique(new_df[,by2])){
rows <- which(new_df[,by]== x & new_df[,by2]== y)
new_df[rows, new_col] <- sum(new_df[rows,"suicides_no"]) / sum(new_df[rows,"population"]) * 100000
}}
return(new_df)}
df$c_code <- countrycode::countryname(df$country, "iso3c")
df <- aggreGate(df, new_col = "year_country")
df$year_country <- round(df$year_country, 1)
df$hover <- paste(df$country, paste(df$year_country, " /100k", sep = ""), sep = "\n")
label <- list(bgcolor = "black",
bordercolor = "transparent",
font = list(size = 15, color = "white"))
plot_geo(df[df$year>=1989 & df$year<2016,], locationmode = "countries",
frame = ~year,
width = 800, height = 800) %>%
add_trace(locations = ~c_code,
z = ~year_country,
zmin = mean(df$year_country[df$year>=1989 & df$year<2016])/3,
zmax = mean(df$year_country[df$year>=1989 & df$year<2016])*3,
color = ~year_country,
colorscale = "RdBu",
text = ~hover,
hoverinfo = "text") %>%
layout(geo = list(scope = "europe"),
title = list(text = "<b>Yearly suicides in european countries</b>",
y = 0.95, font = list(size = 20)),
margin = list(t = 200)) %>%
style(hoverlabel = label) %>%
colorbar(title = "<b>Suicide</b>", y = 0.8, ticksuffix = "/100k")